package com.lipaluma.examples.xml.custom;

import java.lang.reflect.Field;

import com.lipaluma.context.Context;
import com.lipaluma.field.mapper.FieldMapper;
import com.lipaluma.field.mapper.initializer.XmlFieldMapperInitializer;
import com.lipaluma.field.mapper.mapping.Column;
import com.lipaluma.field.mapper.mapping.MappingInfo;
import com.lipaluma.io.Row;
import com.lipaluma.parser.ObjectMarshalled;
import com.lipaluma.xml.mapping.XmlFieldMapping;

public class ColumnAmountTTC implements FieldMapper, XmlFieldMapperInitializer {

	private Field field;
	private double tva = 19.60;
	private Column mapping;
	
	@Override
	public void init(Context context, Field field, XmlFieldMapping mapping) {
		this.field = field;
		this.mapping = new Column("Amount_Net");
	}
	
	@Override
	public Object unmarshal(Row row) {
		String value = row.extractValue(mapping);
		
		double amountNet = Double.parseDouble(value);
		return amountNet+(amountNet*tva/100);
	}
	@Override
	public void marshalValue(Object value, ObjectMarshalled marshal) {}
	
	@Override
	public String getDescription() {
		return "colonne montant TTC";
	}
	@Override
	public MappingInfo[] getMappings() {
		return new Column[]{mapping};
	}

	@Override
	public Field getField() {
		return field;
	}
}
